<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/">角色管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary">添加角色</el-button>
                </el-col>
            </el-row>

            <el-table border :data=rolesList>
                <el-table-column type="expand">
                    <template slot-scope="{row,$index}">
                        <el-row :class="['bdbottom',i1===0?'bdtop':'']" v-for="(item1,i1) in row.children"
                            :key="item1.id">
                            <!-- 一级权限 -->
                            <el-col :span="5" class="el-col1">
                                <el-tag @close="removeRightById(row,item1.id)" type="primary" closable
                                    :disable-transitions="false">
                                    {{item1.authName}}
                                </el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <!-- 二级权限 -->
                            <el-col :span="19">
                                <el-row :class="['bdbottom',i2===0?'bdtop':'']" v-for="(item2,i2) in item1.children"
                                    :key="item2.id">
                                    <el-col :span="6">
                                        <el-tag @close="removeRightById(row,item2.id)" type="success" closable
                                            :disable-transitions="false">
                                            {{item2.authName}}
                                        </el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <!-- 三级权限 -->
                                    <el-col :span="18">
                                        <el-tag type="warning" closable :disable-transitions="false"
                                        @close="removeRightById(row,item3.id)"
                                         v-for="(item3) in item2.children" :key="item3.id">{{item3.authName}}
                                        </el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>

                <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
                <el-table-column prop="roleName" label="角色名称"></el-table-column>
                <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="{row,$inex}">
                        <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                        <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                        <el-button type="warning" icon="el-icon-setting" size="mini" @click="eidtRole(row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <!-- 编辑权限对话框 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="50%" @close="closeDialog">
            <!-- 树形控件 -->
            <el-tree node-key="id" :default-expand-all="true" ref="treeRef"
                    show-checkbox :data="tressRoleList" :default-checked-keys="defKeys"
                    :props="treeProps">
            </el-tree>

            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="allowRights">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
export default {
  name: 'Roles',

  data () {
    return {
      rolesList: [],
      dialogVisible: false,
      tressRoleList: [],
      // 树形控件的属性绑定对象
      treeProps: {
        // label: 指定节点标签为节点对象的某个属性值
        label: 'authName',
        // 指定子树为节点对象的某个属性值
        children: 'children'
      },
      // 默认一开始就选中的权限
      defKeys: [],
      roleId: ''
    }
  },

  created () {
    this.getAllRoles()
  },

  methods: {
    async getAllRoles () {
      const { data, meta } = await this.$http.roles.getAllRoles()
      if (meta.status === 200) {
        this.rolesList = data
      }
    },

    // 根据id删除用户权限
    async removeRightById (role, rightId) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)

      if (res === 'confirm') {
        const { data, meta } = await this.$http.roles.updataRoleById(role.id, rightId)
        if (meta.status === 200) {
          this.$message.success('删除用户权限成功')

          role.children = data
        } else {
          this.$message.error('删除用户权限失败')
        }
      }
    },

    // 打开编辑权限对话框
    async eidtRole (role) {
      // 获取当前role的Id,以供之后allowRights方法使用
      this.roleId = role.id
      // 获取所有权限的数据
      const { data, meta } = await this.$http.roles.getAllRolesByTree()
      if (meta.status === 200) {
        this.tressRoleList = data
      }

      this.getLeafKeys(role, this.defKeys)

      this.dialogVisible = true
    },

    // 添加默认的以及存在的权限
    getLeafKeys (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }

      node.children.forEach(item => this.getLeafKeys(item, arr))
    },

    // 关闭编辑权限
    closeDialog () {
      this.defKeys = []
    },

    // 根据树形组件对象id设置用户权限
    async allowRights () {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]

      const idStr = keys.join(',')

      const { meta } = await this.$http.roles.setUserRoleById(this.roleId, idStr)
      if (meta.status === 200) {
        this.$message.success('分配用户权限成功')
        this.getAllRoles()
      } else {
        this.$message.error('分配用户权限失败')
      }

      this.dialogVisible = false
    }
  }

}
</script>

<style lang="less" scoped>
    .el-row {
        margin-bottom: 20px;
        display: flex;
        align-items: center;
    }
    .el-col1 {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .el-tag {
        margin: 7px;
    }
    .bdtop {
        border-top: 1px solid #eee;
    }
    .bdbottom {
        border-bottom: 1px solid #eee;
    }
    .vcenter {
        display: flex;
        align-items: center;
    }

</style>
